package com.beself.until;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class PareDate {

    private static SimpleDateFormat sdf ;

    /**
     * 转换成字符串
     *  date  传入的时间
     *  choose 传入转换成那种日期的格式
     */
    public String toString(Date date,Integer choose) {
        if (date == null) {
            date = new Date();
        }
        if (choose ==null){
            choose = 1;
        }
        String str = "";
        switch (choose){
            case 1:
                str = "yyyy-MM-dd";
                break;
            case 2:
                str="yyyy/MM/dd";
                break;
            case 3:
                str="yyyy-MM-dd hh:mm:ss";
                break;
        }
        sdf = new SimpleDateFormat(str);
        String format = sdf.format(date);
        return format;
    }

    /**
     * 转换成日期
     */
    public Date pareDate(String str){
        Date result = null;
        if (str == null && str.trim().length() == 0 ){
            Date date = new Date();
        }
        sdf = new  SimpleDateFormat("yyyy-MM-dd");
        try {
         result   = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     *  获取会员更新的时间
     * @param day
     * @return
     */
    public static String getStatetime(int day){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, day);
        Date monday = c.getTime();
        sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String preMonday = sdf.format(monday);
        return preMonday;
    }
}